package com.SRMS.service;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @author Albert
 * @version 1.0
 * @date 2023/12/24-14:07
 * @describe 该类用于管理和客户端通信的线程
 */
public class ManageClientThreads {
    private static HashMap<String, ServerConnectClientThread> hm = new HashMap<>();

    public static HashMap<String, ServerConnectClientThread> getHm() {
        return hm;
    }

    //添加线程对象到hm集合
    public static void addServerConnectClientThread(String userId, ServerConnectClientThread serverConnectClientThread){
        hm.put(userId, serverConnectClientThread);
    }

    //根据用户id获取ServerConnectClientThread对象
    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return hm.get(userId);
    }

    //这里编写方法，可以返回在线用户列表
    public static String getOnlineUser(){
        //遍历集合的key
        Iterator<String> iterator = hm.keySet().iterator();
        String onlineUserList = "";
        while (iterator.hasNext()) {
            onlineUserList +=  iterator.next().toString() + " ";
        }
        return onlineUserList;
    }

    //将线程从hm集合中移除
    public static void removeServerConnectClientThread(String userId){
        hm.remove(userId);
    }
}
